package com.lvkerry.generate;


import com.lvkerry.generate.bean.MenuBean;
import com.lvkerry.generate.bean.OrderBean;
import com.lvkerry.generate.bean.UserBean;
import com.lvkerry.generate.bean.UserDeskBean;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author Kerry on 18/03/07
 */

public class OrderGenete {
    private Set<UserBean> users;
    private List<MenuBean> menus;
    private Set<UserDeskBean> userDesks;

    public OrderGenete(Set<UserBean> users, List<MenuBean> menus, Set<UserDeskBean> userDesks) {
        this.users = users;
        this.menus = menus;
        this.userDesks = userDesks;
    }


    public Integer findDeskByUserId(Long userId) {
        for (UserDeskBean desk : userDesks) {
            if (desk.getUserId().equals(userId)) {
                return desk.getTableNo();
            }
        }
        return 0;
    }


    public MenuBean getMenu() {
        Random random = new Random();
        return menus.get(random.nextInt(this.menus.size()));
    }


    public Set<OrderBean> addOrderData(int orderCount) {
        LocalDate startDate = LocalDate.now().minusDays(orderCount);

        Set<OrderBean> orders = new LinkedHashSet<>();

        for (int i = 0; i <= orderCount; i++) {
            LocalDate localDate = startDate.plusDays((long) i);
            String lunchDate = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

            for (UserBean user : users) {
                MenuBean menu = getMenu();
                OrderBean orderBean = new OrderBean();
                orderBean.setLunchDate(lunchDate);

                orderBean.setMenuAvatarUrl(menu.getAvatarUrl());
                orderBean.setMenuId(menu.getMenuId());
                orderBean.setMenuName(menu.getName());

                orderBean.setName(user.getName());
                orderBean.setOrderNo("ABA-" + lunchDate + "-" + user.getUsername());
                orderBean.setOrderStatus(0);
                orderBean.setOrderTime(localDate.atStartOfDay(ZoneId.systemDefault()).toEpochSecond() * 1000L + 39600000);
                orderBean.setPrice(menu.getPrice());
                orderBean.setUserId(user.getUserId());
                orderBean.setUsername(user.getUsername());

                Integer deskByUserId = findDeskByUserId(user.getUserId());
                orderBean.setTableNo(deskByUserId);

                String remark = menu.getRemark();
                if (remark != null) {
                    String[] split = remark.split(",");
                    if (split.length > 0) {
                        int v = (int) (Math.random() * split.length);
                        orderBean.setRemark(split[v]);
                    }
                }
                orders.add(orderBean);
            }
        }

        return orders;
    }


}
